package dymnd;
import java.awt.event.*;

import jpen.PKind.Type;
import jpen.event.PenListener;
import jpen.*;
import java.awt.*;
public class TabletAdapter extends MouseMotionAdapter implements PenListener{

	int x = 0;
	int y = 0;
	double pressure = 1;
	boolean button = false;
	boolean tablet = false;
	public TabletAdapter(Component c)	{		
		try	{
			PenManager pm = new PenManager(c);
			pm.pen.setFrequency(100);
			//System.out.println(pm.pen.DEFAULT_FREQUENCY);
			pm.pen.addListener(this);
		}	catch(Exception x)	{
			x.printStackTrace();
			System.out.println("Bug in JPen - Or the way it's accessed");
		}
	}
	
	public void penButtonEvent(PButtonEvent ev) {
		//System.out.println(ev);
		button = ev.button.value;
		tabletReleased(x, y, pressure);
		
	}
	//@Override
	public void penKindEvent(PKindEvent ev) {
		//System.out.println(ev);
		if(ev.kind.getType().equals(PKind.Type.STYLUS))	
			tablet = true;
		else
			tablet = false;
		
	}
	//@Override
	public void penLevelEvent(PLevelEvent ev) {
		x =(int) ev.pen.getLevelValue(PLevel.Type.X);
		y =(int) ev.pen.getLevelValue(PLevel.Type.Y);
		pressure = ev.pen.getLevelValue(PLevel.Type.PRESSURE);
		
		//System.out.println(pressure);
		if(pressure == 0 && ev.pen.hasPressedButtons())	{
			pressure = 1;
		}
		
		
		if(ev.pen.getButtonValue(PButton.Type.LEFT))	{ 		
			pressureEvent(pressure);
			
		}
	}
	
	//@Override
	public void penScrollEvent(PScrollEvent ev) {
		//System.out.println(ev);
	}
	//@Override
	public void penTock(long availableMillis) {
		//System.out.println("TOCK - available period fraction: "+availableMillis);
	}
	
	
	
	public void pressureEvent(double pressure)	{}
	
	public void tabletDragged(int x, int y, double pressure)	{}
		
	public void tabletReleased(int x, int y, double pressure)	{}
	
}
